<template>
  <!-- 设备导入 -->
  <el-dialog :title="$t('store.nearby.importStores')" :visible.sync="upload.open" width="400px" append-to-body>
    <el-button type="primary" @click="importTemplate()" style="margin-bottom: 10px">{{$t('device.center.import.downloadTemplate')}}</el-button>

    <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url"
      :disabled="upload.isUploading" :on-success="handleFileSuccess" :on-progress="handleFileUploadProgress"
      :auto-upload="false" drag :data="uploadForm">
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">
        {{$t('device.center.import.uploadText')}}
        <em>{{$t('device.center.import.clickUpload')}}</em>
      </div>
      <div class="el-upload__tip" style="color: red" slot="tip">
        {{$t('device.center.import.uploadTip')}}
      </div>
    </el-upload>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="uploadSubmit" :loading="upload.isUploading">{{$t('common.button.confirm')}}</el-button>
      <el-button @click="upload.open = false">{{$t('common.button.cancel')}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getToken } from "@/utils/auth";
import { typeTree } from "@/api/device/type";
import { modelTree } from "@/api/device/model";

export default {
  name: "centerImport",
  data() {
    return {
      deviceTypeOptions: [],
      deviceModelOptions: [],
      upload: {
        open: false,
        isUploading: false,
        headers: {
          Authorization: "Bearer " + getToken()
        },
        url: process.env.VUE_APP_BASE_API + "/device/ecu/importData",
      },
      uploadForm: {
        
      },
      uploadRules: {
        deviceTypeId: [
          {required: true, message: this.$t('device.center.import.uploadRules.deviceTypeId'), trigger: "change",},
        ],
        deviceModelId: [
          {required: true, message: this.$t('device.center.import.uploadRules.deviceModelId'), trigger: "change",},
        ],
        file: "",
      },
    }
  },
  methods: {
    init() {
      this.getTypeTree();
      this.upload.open = true
    },
    deviceTypeChange(typeId) {
      if (typeId) {
        this.getModelTree(typeId)
      } else {
        this.uploadForm.deviceModelId = undefined
        this.deviceModelOptions = []
      }
    },
    getTypeTree() {
      typeTree().then(response => {
        this.deviceTypeOptions = response.data;
      });
    },
    getModelTree(typeId) {
      modelTree(typeId).then(response => {
        this.deviceModelOptions = response.data;
      });
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('/device/ecu/importTemplate', {
      }, `store_template_${new Date().getTime()}.xlsx`)
    },
    uploadSubmit() {
          this.$refs.upload.submit();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.resetForm("uploadForm");
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", this.$t('common.alert.importResult'), { dangerouslyUseHTMLString: true });
      this.refreshData();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    refreshData() {
      this.$emit("refreshData")
    },
  },
}
</script>

<style scoped>

</style>
